package com.skillworld.webapp.model.enrollment;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.skillworld.webapp.model.enrollment.ReleasedExam.Grade;
import com.skillworld.webapp.model.enrollment.ReleasedExam.State;
import com.skillworld.webapp.model.lesson.Exam;
import com.skillworld.webapp.model.lesson.Lesson;
import com.skillworld.webapp.model.user.User;

@Entity
@Table(name="StudentLesson")
public class Examination
{
	private Long examinationId;
    private Enrollment enrollment;
	private Lesson lesson;
	private Calendar finishDate;
	private ReleasedExam releasedExam;
	
	
	protected Examination()
	{ 

	}

	public Examination(Enrollment enrollment, Lesson lesson)
	{
		this.enrollment = enrollment;
		this.lesson = lesson;
		this.finishDate = null;
		this.releasedExam = new ReleasedExam();
	}

	
	@SequenceGenerator(name="ExaminationIdGenerator",sequenceName="ExaminationSeq")
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO,generator="ExaminationIdGenerator")
    @Column(name="idStudentLesson")
	public Long getExaminationId() 
	{
        return examinationId;
    }
	
	protected void setExaminationId(Long examinationId) 
	{
        this.examinationId = examinationId;
    }

	@ManyToOne(optional=false,fetch=FetchType.LAZY)
    @JoinColumn(name="idEnrollment")
	public Enrollment getEnrollment()
	{
        return enrollment;
    }

    protected void setEnrollment(Enrollment enrollment)
    {
        this.enrollment = enrollment;
    }
    
    @Transient
    public User getStudent()
    {
        return getEnrollment().getStudent();
    }

    @ManyToOne(optional=false,fetch=FetchType.LAZY)
	@JoinColumn(name="idLesson")
	public Lesson getLesson()
	{
        return lesson;
    }
	
	protected void setLesson(Lesson lesson)
	{
        this.lesson = lesson;
    }
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="finishDate")
	public Calendar getFinishDate()
	{
        return finishDate;
    }
	
	public void setFinishDate(Calendar finishDate)
	{
        this.finishDate = finishDate;
    }
	
	@Transient
	public boolean isFinished()
	{
	    return getFinishDate() != null;
	}
	
	@Embedded
	public ReleasedExam getReleasedExam()
	{
        return releasedExam;
    }
	
	protected void setReleasedExam(ReleasedExam releasedExam)
	{
        this.releasedExam = releasedExam;
    }
	
	public void newExam(Exam exam)
	{
	    this.releasedExam = new ReleasedExam(exam);
	}
	
    public void answer(String responseFile)
    {
        releasedExam.setState(State.ANSWERED);
        releasedExam.setSubmittedDate(Calendar.getInstance());
        releasedExam.setResponseFile(responseFile);
    }
    
    void evaluate(Grade grade, String commentary)
    {
        Calendar now = Calendar.getInstance();
        
        releasedExam.setGrade(grade);
        releasedExam.setEvaluationDate(now);
        releasedExam.setCommentary(commentary);
        
        if(releasedExam.getState() == State.PASSED)
        { finishDate = now; }
    }
}
